package cn.adelyn.blog.file.service;

import cn.adelyn.base.api.snowfalke.feign.SnowflakeFeignClient;
import cn.adelyn.blog.file.constant.MinioConstant;
import cn.adelyn.blog.file.dao.FileInfoDAOService;
import cn.adelyn.blog.file.pojo.dto.InsertFileInfoDTO;
import cn.adelyn.blog.file.pojo.dto.UpdateFileInfoDTO;
import cn.adelyn.blog.file.pojo.model.FileInfoModel;
import cn.adelyn.blog.file.pojo.vo.FileUrlVO;
import cn.adelyn.common.core.cglib.CglibUtil;
import cn.adelyn.common.core.execption.AdelynException;
import cn.adelyn.common.core.response.ResponseEnum;
import cn.adelyn.common.oss.config.MinioTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;

@Service
public class FileService {

    @Autowired
    FileInfoDAOService fileInfoDAOService;

    @Autowired
    MinioTemplate minioTemplate;

    @Autowired
    SnowflakeFeignClient snowflakeFeignClient;

    public void insertFileInfo(InsertFileInfoDTO insertFileInfoDTO, Long userId) {
        FileInfoModel fileInfoModel = CglibUtil.copy(insertFileInfoDTO, FileInfoModel.class);
        fileInfoModel.setUserId(userId);
        fileInfoDAOService.insertFileInfo(fileInfoModel);
    }

    public void updateFileInfo(UpdateFileInfoDTO updateFileInfoDTO) {
        FileInfoModel fileInfoModel = CglibUtil.copy(updateFileInfoDTO, FileInfoModel.class);
        fileInfoDAOService.updateFileInfo(fileInfoModel);
    }

    public void deleteFile(String filePath, Long fileId) {

        try {
            minioTemplate.delete(MinioConstant.BUCKET, filePath);
            fileInfoDAOService.deleteFileInfo(fileId);
        } catch (Exception e) {
            throw new AdelynException(ResponseEnum.FILE_OSS_DELETE_FAIL);
        }
    }

    public FileUrlVO genUploadUrl(String filePath) {
        try {
            Long fileId = snowflakeFeignClient.snowflakeId().getData();
            String uploadUrl = minioTemplate.genUploadUrl(MinioConstant.BUCKET,
                    filePath + MinioConstant.PATH_SEPARATOR + fileId,
                    MinioConstant.UPLOAD_URL_DURATION, MinioConstant.TIME_UNIT);
            return new FileUrlVO(fileId, uploadUrl);
        } catch (Exception e){
            throw new AdelynException(ResponseEnum.FILE_UPLOAD_URL_GEN_FAIL, e);
        }
    }

    public String genDownloadUrl(String filePath) {
        try {
            String downloadUrl = minioTemplate.genDownloadUrl(MinioConstant.BUCKET, filePath,
                    MinioConstant.DOWNLOAD_URL_DURATION, MinioConstant.TIME_UNIT);
            return downloadUrl;
        } catch (Exception e){
            throw new AdelynException(ResponseEnum.FILE_DOWNLOAD_URL_GEN_FAIL, e);
        }
    }

    public long putFile(InputStream is, String fileName, String filePathPrefix, String contentType, long userId) {
        long fileId = snowflakeFeignClient.snowflakeId().getData();

        InsertFileInfoDTO insertFileInfoDTO = new InsertFileInfoDTO();
        insertFileInfoDTO.setFileId(fileId);
        insertFileInfoDTO.setFileName(fileName);
        insertFileInfo(insertFileInfoDTO, userId);

        try {
            minioTemplate.add(is, MinioConstant.BUCKET, filePathPrefix + fileId, contentType);
        } catch (Exception e) {
            throw new AdelynException("添加文件对象失败: " + e.getMessage());
        }

        return fileId;
    }

    public InputStream getFile(String filePath) {
        try {
            return minioTemplate.get(MinioConstant.BUCKET, filePath);
        } catch (Exception e) {
            throw new AdelynException("获取文件对象失败: " + e.getMessage());
        }
    }

}
